<template>
    <div class="header-container">
      <!-- 左侧面包屑 -->
      <div class="left-content">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item 
            v-for="(item, index) in breadcrumbs" 
            :key="index"
            :to="item.path"
          >
            {{ item.meta.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
  
      <!-- 右侧功能区 -->
      <div class="right-content">
        <!-- 全屏功能 -->
        <el-tooltip effect="dark" content="全屏" placement="bottom">
          <el-icon class="action-item" @click="toggleFullScreen">
            <FullScreen />
          </el-icon>
        </el-tooltip>
  
        <!-- 通知中心 -->
        <el-popover placement="bottom" :width="300" trigger="click">
          <template #reference>
            <el-badge :value="5" class="badge-item">
              <el-icon class="action-item"><Bell /></el-icon>
            </el-badge>
          </template>
          <notification-list />
        </el-popover>
  
        <!-- 用户信息 -->
        <el-dropdown @command="handleCommand">
          <div class="user-info">
            <el-avatar :size="30" :src="user.avatar" />
            <span class="user-name">{{ user.name }}</span>
            <el-icon class="arrow-down"><ArrowDown /></el-icon>
          </div>
          
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">
                <el-icon><User /></el-icon>个人中心
              </el-dropdown-item>
              <el-dropdown-item command="settings">
                <el-icon><Setting /></el-icon>系统设置
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <el-icon><SwitchButton /></el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </template>
  
  <script setup>
  import { computed } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import { useUserStore } from '@/stores/user'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import NotificationList from './NotificationList.vue'
  import {
    FullScreen,
    Bell,
    ArrowDown,
    User,
    Setting,
    SwitchButton
  } from '@element-plus/icons-vue'
  
  const route = useRoute()
  const router = useRouter()
  const userStore = useUserStore()
  
  // 面包屑导航
  const breadcrumbs = computed(() => {
    const matched = route.matched.filter(item => item.meta?.title)
    return matched.map(item => ({
      path: item.path,
      meta: item.meta
    }))
  })
  
  // 用户信息
  const user = computed(() => ({
    name: userStore.userInfo?.name || '未登录',
    avatar: userStore.userInfo?.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
  }))
  
  // 处理下拉菜单命令
  const handleCommand = async (command) => {
    switch (command) {
      case 'logout':
        await handleLogout()
        break
      case 'profile':
        router.push('/profile')
        break
      case 'settings':
        router.push('/settings')
        break
    }
  }
  
  // 退出登录处理
  const handleLogout = () => {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout()
      router.push('/login')
      ElMessage.success('退出成功')
    })
  }
  
  // 全屏切换
  const toggleFullScreen = () => {
    if (!document.fullscreenElement) {
      document.documentElement.requestFullscreen()
    } else {
      if (document.exitFullscreen) {
        document.exitFullscreen()
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .header-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    padding: 0 20px;
  
    .left-content {
      :deep(.el-breadcrumb) {
        font-size: 14px;
      }
    }
  
    .right-content {
      display: flex;
      align-items: center;
      gap: 20px;
  
      .action-item {
        font-size: 20px;
        cursor: pointer;
        color: #5a5e66;
        transition: color 0.3s;
  
        &:hover {
          color: var(--el-color-primary);
        }
      }
  
      .badge-item {
        :deep(.el-badge__content) {
          top: 12px;
        }
      }
  
      .user-info {
        display: flex;
        align-items: center;
        gap: 8px;
        cursor: pointer;
        padding: 5px;
        border-radius: 4px;
        transition: all 0.3s;
  
        &:hover {
          background: var(--el-color-primary-light-9);
        }
  
        .user-name {
          font-size: 14px;
          color: #606266;
        }
  
        .arrow-down {
          font-size: 12px;
          color: #909399;
        }
      }
    }
  }
  </style>